﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using community.LanFen;


namespace community
{
    public partial class usedrug : Form
    {
        string strSql;
        int drugXLH = 0;
        sql_database sd = new sql_database();
        SqlConnection conn;
      
        public usedrug(Form f)
        {
            InitializeComponent();
        }
        protected override bool ProcessDialogKey(Keys keyData)//按enter键时，光标跳到下一个控件输入
        {
            if (keyData == Keys.Enter)//&& !(ActiveControl is Button))
            {//碰到Button时不跳转到下一个控件，而是执行Button的单击事件
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

        private void usedrug_Load(object sender, EventArgs e)
        {
            dfzy.ReadOnly = true;
            showd();


        }
        private void showd()
        {
            strSql = "select SEQDRUGID_INT as 序列号,DRUGNAME_VCHR as 药物名称 ,DRUPFREQUENCY_VCHR as 使用频率,";
            strSql += "DRUPDOSEUNIT_VCHR as 剂量单位,DRUGDOSEONES_VCHR as  一次剂量,DRUGDOSETOTAL_VHCR as 总剂量,";
            strSql += "DRUGUSEAGE_VCHR as 用法,DRUGREACTIONS_INT as 是否副作用,DRUGREACTIONS_VCHR as 副作用,";
            strSql += "DRUGDATE_VCHR as 用药时间  from T_CS_HYPERTENSIONRECORD_DRUG where SEQID_INT='" + residentcode.hyperXLH + "'";
            strSql += "  and INHABITANTCODE_CHR='" +residentcode.rescode+ "'";
            sd.show_dataGridview(strSql, "T_CS_HYPERTENSIONRECORD_DRUG", showdrug);
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Control ctrl in this.Controls)
                if (ctrl is TextBox || ctrl is ComboBox)
                    ctrl.Text = "";
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int i_XLH = 00001;//初始化药物的序列号
            string str = "select max(SEQDRUGID_INT) from T_CS_HYPERTENSIONRECORD_DRUG"; 
            conn = new SqlConnection(SQL.conStr);
            conn.Open();
            SqlCommand cm = new SqlCommand(str, conn);
            string str_XLH = Convert.ToString(cm.ExecuteScalar());
            if (str_XLH == "") drugXLH = i_XLH;
            drugXLH = Convert.ToInt32(str_XLH) + 1;

            string fzy = "否";
            if (raddno.Checked) fzy = "否";
            if (raddyes.Checked) fzy = "是";



            strSql = "insert into T_CS_HYPERTENSIONRECORD_DRUG (SEQID_INT,SEQDRUGID_INT,INHABITANTCODE_CHR,DRUGNAME_VCHR ,";
            strSql += "DRUPFREQUENCY_VCHR,DRUPDOSEUNIT_VCHR,DRUGDOSEONES_VCHR,DRUGDOSETOTAL_VHCR,DRUGUSEAGE_VCHR,";
            strSql += "DRUGREACTIONS_INT,DRUGREACTIONS_VCHR,DRUGDATE_VCHR) values ('";
            strSql += residentcode.hyperXLH + "','" + drugXLH + "','" + residentcode.rescode + "','" + dname.Text.Trim() + "','";
            strSql += dfreq.Text.Trim() + "','" + dunit.Text.Trim() + "','" + done.Text.Trim() + "','" + dall.Text.Trim() + "','";
            strSql += duseage.Text.Trim() + "','" + fzy + "','" + dfzy.Text.Trim() + "','" + dtime.Text.Trim() + "')";
            sd.new_row(strSql);

            showd();
            
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string fzy = "否";
            if (raddno.Checked) fzy = "否";
            if (raddyes.Checked) fzy = "是";
            strSql ="update T_CS_HYPERTENSIONRECORD_DRUG set DRUGNAME_VCHR= '"+dname.Text.Trim() + "',DRUPFREQUENCY_VCHR='";
            strSql += dfreq.Text.Trim() + "',DRUPDOSEUNIT_VCHR='"+ dunit.Text.Trim() + "',DRUGDOSEONES_VCHR='";
            strSql +=done.Text.Trim() + "',DRUGDOSETOTAL_VHCR='"+ dall.Text.Trim() + "',DRUGUSEAGE_VCHR='";
            strSql +=duseage.Text.Trim() + "',DRUGREACTIONS_INT= '"+ fzy + "',DRUGREACTIONS_VCHR= '"; 
            strSql += dfzy.Text.Trim() + "',DRUGDATE_VCHR='"  + dtime.Text.Trim() + "' where  INHABITANTCODE_CHR='" + residentcode.rescode + "'";
            strSql += "  and SEQID_INT='" + residentcode.hyperXLH + "'  and  SEQDRUGID_INT  ='" +drugXLH + "'"; 
            DialogResult jieguo = MessageBox.Show("您确认要修改吗?", "修改提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (jieguo == DialogResult.OK)
            {
                resident rd = new resident();
                rd.new_row(strSql);
                MessageBox.Show("修改成功");
            }
            else  MessageBox.Show("您已取消修改");
            showd();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            strSql = "delete  from  T_CS_HYPERTENSIONRECORD_DRUG  where  INHABITANTCODE_CHR='" + residentcode.rescode+ "'";
            strSql += "  and SEQID_INT='" + residentcode.hyperXLH + "'  and  SEQDRUGID_INT  ='" +drugXLH + "'";
            DialogResult jieguo = MessageBox.Show("您确认要删除吗?", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (jieguo == DialogResult.OK)
            {
                resident rd = new resident();
                rd.new_row(strSql);
                MessageBox.Show("数据已被删除");
            }
            else MessageBox.Show("您已取消删除");
            showd();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            this.DialogResult = DialogResult.OK;
        }

        private void raddno_CheckedChanged(object sender, EventArgs e)
        {
            dfzy.ReadOnly = true;
        }

        private void raddyes_CheckedChanged(object sender, EventArgs e)
        {
            dfzy.ReadOnly = false;
        }

        private void showdrug_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (showdrug.Rows[e.RowIndex].Cells[0].Value !=DBNull .Value)
                     drugXLH = Convert.ToInt32(showdrug.Rows[e.RowIndex].Cells[0].Value);
                dname.Text = showdrug.Rows[e.RowIndex].Cells[1].Value.ToString();
                dfreq.Text = showdrug.Rows[e.RowIndex].Cells[2].Value.ToString();
                dunit.Text = showdrug.Rows[e.RowIndex].Cells[3].Value.ToString();
                done.Text = showdrug.Rows[e.RowIndex].Cells[4].Value.ToString();
                dall.Text = showdrug.Rows[e.RowIndex].Cells[5].Value.ToString();
                duseage.Text = showdrug.Rows[e.RowIndex].Cells[6].Value.ToString();
                string s = showdrug.Rows[e.RowIndex].Cells[7].Value.ToString();
                if (s == "否")
                {
                    raddno.Checked = true;
                    dfzy.ReadOnly = true;
                }

                else
                {
                    raddyes.Checked = true;
                    dfzy.ReadOnly = false;
                    dfzy.Text = showdrug.Rows[e.RowIndex].Cells[8].Value.ToString();
                }
                dtime.Text = showdrug.Rows[e.RowIndex].Cells[9].Value.ToString();
                
            }
        }

         
    }
}
